package com.gwcm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gwcm.exception.BusinessException;
import com.gwcm.model.dto.product.ProductAddRequest;
import com.gwcm.model.dto.product.ProductDeleteRequest;
import com.gwcm.model.dto.product.ProductQueryRequest;
import com.gwcm.model.dto.product.ProductUpdateRequest;
import com.gwcm.model.entity.Product;
import com.gwcm.model.vo.ProductVo;
import com.gwcm.service.ProductService;
import com.yhz.commonutil.common.BaseResponse;
import com.yhz.commonutil.common.ErrorCode;
import com.yhz.commonutil.common.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
@Api(tags = "商品接口")
public class ProductController {
    @Autowired
    public ProductService productService;

    @ApiOperation("新增商品")
    @PreAuthorize("hasAuthority('pos:product:add')")
    @PostMapping("/save")
    public BaseResponse addProduct(@RequestBody ProductAddRequest productAddRequest) {
        Product product = new Product();
        BeanUtils.copyProperties(productAddRequest, product);

        boolean save = productService.save(product);
        if (!save) {
            throw new BusinessException(ErrorCode.SAVE_ERROR);
        }
        return ResultUtils.success();
    }

    @ApiOperation("分页查询商品接口")
    @PreAuthorize("hasAuthority('pos:product:list')")
    @PostMapping("/page")
    public BaseResponse<IPage<ProductVo>> pageModuleTopic(@RequestBody ProductQueryRequest productQueryRequest) throws BusinessException {
        IPage<ProductVo> productVoIPage = productService.pageList(productQueryRequest);
        return ResultUtils.success(productVoIPage);
    }

    @ApiOperation("修改商品")
    @PreAuthorize("hasAuthority('pos:product:edit')")
    @PostMapping("/update")
    public BaseResponse updateProduct(@RequestBody ProductUpdateRequest productUpdateRequest) {
        Product product = new Product();
        BeanUtils.copyProperties(productUpdateRequest, product);

        boolean update = productService.updateById(product);
        if (!update) {
            throw new BusinessException(ErrorCode.UPDATE_ERROR);
        }
        return ResultUtils.success();
    }

    @ApiOperation("删除商品")
    @PreAuthorize("hasAuthority('pos:product:delete')")
    @PostMapping("/delete")
    public BaseResponse deleteProduct(@RequestBody ProductDeleteRequest productDeleteRequest) {
        Product product = new Product();
        BeanUtils.copyProperties(productDeleteRequest, product);

        boolean delete = productService.removeById(product);
        if (!delete) {
            throw new BusinessException(ErrorCode.DELETE_ERROR);
        }
        return ResultUtils.success();
    }
}
